﻿using System;
using MySql.Data.MySqlClient;

public class DatabaseTest
{
    public static void Main(string[] args)
    {
        string connectionString = "Server=101.42.223.13;Database=test;Uid=root;Pwd=Gaoyuyang00;SslMode=None;AllowLoadLocalInfile=true;AllowUserVariables=true;";
        
        Console.WriteLine("尝试连接数据库...");
        Console.WriteLine($"连接字符串: {connectionString}");
        
        try
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                Console.WriteLine("正在打开连接...");
                connection.Open();
                Console.WriteLine("✅ 数据库连接成功！");
                
                // 测试查询
                Console.WriteLine("\n测试查询...");
                using (var command = new MySqlCommand("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'test'", connection))
                {
                    var result = command.ExecuteScalar();
                    Console.WriteLine($"数据库 'test' 中的表数量: {result}");
                }
                
                // 检查知识库相关表
                Console.WriteLine("\n检查知识库相关表...");
                using (var command = new MySqlCommand("SHOW TABLES LIKE '%knowledge%'", connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        Console.WriteLine("知识库相关表:");
                        while (reader.Read())
                        {
                            Console.WriteLine($"  - {reader[0]}");
                        }
                    }
                }
                
                // 检查客户服务相关表
                Console.WriteLine("\n检查客户服务相关表...");
                using (var command = new MySqlCommand("SHOW TABLES LIKE '%customer%'", connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        Console.WriteLine("客户服务相关表:");
                        while (reader.Read())
                        {
                            Console.WriteLine($"  - {reader[0]}");
                        }
                    }
                }
                
                connection.Close();
                Console.WriteLine("连接已关闭");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"❌ 数据库连接失败: {ex.Message}");
            Console.WriteLine($"详细错误: {ex}");
            
            if (ex.InnerException != null)
            {
                Console.WriteLine($"内部错误: {ex.InnerException.Message}");
            }
        }
        
        Console.WriteLine("\n按任意键退出...");
        Console.ReadKey();
    }
}